<?php
class ModelSaleGiftcard extends Model {
	public function addGiftcard($data) {
		//$code = substr(md5(rand()), 0, 9);
		$code = $data['code'];
      	$this->db->query("INSERT INTO " . DB_PREFIX . "giftcard SET code = '" . $this->db->escape($code) . "', from_name = '" . $this->db->escape($data['from_name']) . "', from_email = '" . $this->db->escape($data['from_email']) . "', to_name = '" . $this->db->escape($data['to_name']) . "', to_email = '" . $this->db->escape($data['to_email']) . "', message = '" . $this->db->escape($data['message']) . "', amount = '" . (float)$data['amount'] . "', status = '" . (int)$data['status'] . "', date_added = NOW()");
		
		$this->sendMail($data);
	}
	
	public function editGiftcard($giftcard_id, $data) {
      	$this->db->query("UPDATE " . DB_PREFIX . "giftcard SET code = '" . $this->db->escape($data['code']) . "', from_name = '" . $this->db->escape($data['from_name']) . "', from_email = '" . $this->db->escape($data['from_email']) . "', to_name = '" . $this->db->escape($data['to_name']) . "', to_email = '" . $this->db->escape($data['to_email']) . "', message = '" . $this->db->escape($data['message']) . "', amount = '" . (float)$data['amount'] . "', status = '" . (int)$data['status'] . "' WHERE giftcard_id = '" . (int)$giftcard_id . "'");
		
		if(isset($data['send_giftcard_email']) && $data['send_giftcard_email']){
			$this->sendMail($data);
		}
	}
	
	public function deleteGiftcard($giftcard_id) {
      	$this->db->query("DELETE FROM " . DB_PREFIX . "giftcard WHERE giftcard_id = '" . (int)$giftcard_id . "'");
		$this->db->query("DELETE FROM " . DB_PREFIX . "giftcard_history WHERE giftcard_id = '" . (int)$giftcard_id . "'");
	}
	
	public function getGiftcard($giftcard_id) {
      	$query = $this->db->query("SELECT DISTINCT * FROM " . DB_PREFIX . "giftcard WHERE giftcard_id = '" . (int)$giftcard_id . "'");
		
		return $query->row;
	}
	
	public function getGiftcards($data = array()) {
		//$sql = "SELECT v.giftcard_id, v.order_id, v.code, v.from_name, v.from_email, v.to_name, v.to_email, v.amount, (SELECT vtd.name FROM " . DB_PREFIX . "giftcard_theme_description vtd WHERE vtd.giftcard_theme_id = v.giftcard_theme_id AND vtd.language_id = '" . (int)$this->config->get('config_language_id') . "' ) AS theme, v.status, v.date_added FROM " . DB_PREFIX . "giftcard v";
		$sql = "SELECT v.giftcard_id, v.order_id, v.code, v.from_name, v.from_email, v.to_name, v.to_email, v.amount, v.status, v.date_added FROM " . DB_PREFIX . "giftcard v";
		
		$sort_data = array(
			'v.code',
			'v.from_name',
			'v.from_email',
			'v.to_name',
			'v.to_email',
			'v.amount',
			//'v.theme',
			'v.status',
			'v.date_added',
			'v.order_id',
		);	
			
		if (isset($data['sort']) && in_array($data['sort'], $sort_data)) {
			$sql .= " ORDER BY " . $data['sort'];	
		} else {
			$sql .= " ORDER BY v.date_added";	
		}
			
		if (isset($data['order']) && ($data['order'] == 'DESC')) {
			$sql .= " DESC";
		} else {
			$sql .= " ASC";
		}
		
		if (isset($data['start']) || isset($data['limit'])) {
			if ($data['start'] < 0) {
				$data['start'] = 0;
			}			

			if ($data['limit'] < 1) {
				$data['limit'] = 20;
			}	
			
			$sql .= " LIMIT " . (int)$data['start'] . "," . (int)$data['limit'];
		}		
		
		$query = $this->db->query($sql);
		
		return $query->rows;
	}
	
	public function getGiftcardsByOrderId($order_id) {
		$query = $this->db->query("SELECT v.giftcard_id, v.code, v.from_name, v.from_email, v.to_name, v.to_email, v.amount, v.status, v.date_added FROM " . DB_PREFIX . "giftcard v WHERE v.order_id = '" . (int)$order_id . "'");
				
		return $query->rows;
	}
			
	public function getTotalGiftcards() {
      	$query = $this->db->query("SELECT COUNT(*) AS total FROM " . DB_PREFIX . "giftcard");
		
		return $query->row['total'];
	}	
	
	public function getTotalGiftcardsByGiftcardThemeId($giftcard_theme_id) {
      	//$query = $this->db->query("SELECT COUNT(*) AS total FROM " . DB_PREFIX . "giftcard WHERE giftcard_theme_id = '" . (int)$giftcard_theme_id . "'");
		
		//return $query->row['total'];
		return null;
	}	
	
	public function getGiftcardHistories($giftcard_id, $start = 0, $limit = 10) {
		$sql = "SELECT vh.order_id, CONCAT(o.firstname, ' ', o.lastname) AS customer, vh.amount, vh.date_added " .
			" FROM " . DB_PREFIX . "giftcard_history vh " .
			" LEFT JOIN `" . DB_PREFIX . "order` o ON (vh.order_id = o.order_id) " .
			" WHERE vh.giftcard_id = '" . (int)$giftcard_id . "' " .
			" ORDER BY vh.date_added ASC LIMIT " . (int)$start . "," . (int)$limit;
		$query = $this->db->query($sql);

		return $query->rows;
	}
	
	public function getTotalGiftcardHistories($giftcard_id) {
	  	$query = $this->db->query("SELECT COUNT(*) AS total FROM " . DB_PREFIX . "giftcard_history WHERE giftcard_id = '" . (int)$giftcard_id . "'");

		return $query->row['total'];
	}
	
	private function sendMail($giftcard){
		$language_query = $this->db->query("SELECT filename, directory FROM `" . DB_PREFIX . "language` WHERE language_id = '" . (int) $this->config->get('config_language_id') . "'");
		
		if($language_query->num_rows){
			$this->load->model('localisation/language');
			
			$language = new Language($language_query->row['directory']);
			$language->load($language_query->row['filename']);	
			$language->load('mail/giftcard');
			
			//$sql = "SELECT * FROM `" . DB_PREFIX . "giftcard`  WHERE giftcard_id = '" . (int)$giftcard_id . "'";            
			//$giftcard_query = $this->db->query($sql);
						
			// HTML Mail
			$template = new Template();
			
			$template->data['title'] = $language->get('text_title');				
			$template->data['text_greeting'] = sprintf($language->get('text_greeting'), $giftcard['to_name'], $giftcard['from_name']);
			$template->data['text_from'] = sprintf($language->get('text_from'), $giftcard['from_name']);
			$template->data['text_message'] = $language->get('text_message');
			$template->data['text_redeem'] = sprintf($language->get('text_redeem'), $giftcard['code']);
			$template->data['text_footer'] = $language->get('text_footer');				
			
			$template->data['template'] = $this->config->get('config_template');
			$template->data['store_name'] = $this->config->get('config_name');
			$template->data['store_url'] = $this->config->get('config_url');
			
			$template->data['to'] = $giftcard['to_name'];
			$template->data['email'] = $giftcard['to_email'];
			$template->data['message'] = nl2br($giftcard['message']);
			$template->data['order_id'] = $giftcard['order_id'];
			$template->data['giftcard_code'] = $giftcard['code'];
			$template->data['amount'] = nl2br($giftcard['amount']);
					
			$html = $template->fetch('mail/giftcard.tpl');
			
			require_once(DIR_MAILER . 'mwc_mailer.php');        
			$mail = new MwcMailer(); 
			if($this->config->get('config_mail_protocol') == 'smtp'){           
				$mail->Host = $this->config->get('config_smtp_host');
				$mail->Username = $this->config->get('config_smtp_username');
				$mail->Password = $this->config->get('config_smtp_password');
				$mail->Port = $this->config->get('config_smtp_port');            
				
				$mail->IsSMTP();
			}
				
			$mail->setFrom($this->config->get('config_email'), $this->config->get('config_name'));                     
			$mail->Subject = sprintf($language->get('text_subject'), $giftcard['from_name']);
			$mail->MsgHTML($html);                
			$mail->AddAddress($giftcard['to_email']);
			$mail->send();  			
		}
	}
	
	public function sendGiftcard($giftcard_id) {
		$giftcard = $this->getGiftcard($giftcard_id);
		
		if ($giftcard) {
			if($giftcard['order_id']) {
				$order_id = $giftcard['order_id'];
			}
			else {
				$order_id = 0;
			}
			
			$this->load->model('sale/order');			
			$order_info = $this->model_sale_order->getOrder($order_id);
			
			// If giftcard belongs to an order
			if ($order_info) {
				$this->load->model('localisation/language');
				
				$language = new Language($order_info['directory']);
				$language->load($order_info['filename']);	
				$language->load('mail/giftcard');
				
				// HTML Mail
				$template = new Template();				
				
				$template->data['title'] = $language->get('text_title');				
				$template->data['text_greeting'] = sprintf($language->get('text_greeting'), $this->currency->formatPrice($giftcard['amount']));
				$template->data['text_from'] = sprintf($language->get('text_from'), $giftcard['from_name']);
				$template->data['text_message'] = $language->get('text_message');
				$template->data['text_redeem'] = sprintf($language->get('text_redeem'), $giftcard['code']);
				$template->data['text_footer'] = $language->get('text_footer');				
				
				$template->data['template'] = $this->config->get('config_template');
				$template->data['store_name'] = $this->config->get('config_name');
				$template->data['store_url'] = $this->config->get('config_url');
				
				$template->data['to'] = $giftcard['to_name'];
				$template->data['email'] = $giftcard['to_email'];
				$template->data['message'] = nl2br($giftcard['message']);
				$template->data['order_id'] = $order_id;
				$template->data['giftcard_code'] = $giftcard['code'];
				$template->data['amount'] = nl2br($giftcard['amount']);
	
				$html = $template->fetch('mail/giftcard.tpl');
				
				require_once(DIR_MAILER . 'mwc_mailer.php');        
				$mail = new MwcMailer(); 
				if($this->config->get('config_mail_protocol') == 'smtp'){           
					$mail->Host = $this->config->get('config_smtp_host');
					$mail->Username = $this->config->get('config_smtp_username');
					$mail->Password = $this->config->get('config_smtp_password');
					$mail->Port = $this->config->get('config_smtp_port');            
					
					$mail->IsSMTP();
				}
					
				$mail->setFrom($this->config->get('config_email'), $this->config->get('config_name'));                     
				$mail->Subject = sprintf($language->get('text_subject'), $giftcard['from_name']);
				$mail->MsgHTML($html);                
				$mail->AddAddress($giftcard['to_email']);
				$mail->send();  
			}
			// If giftcard does not belong to an order				
			else {
				$language_query = $this->db->query("SELECT filename, directory FROM `" . DB_PREFIX . "language WHERE language_id = '" . (int) $this->config->get('config_language_id') . "'");
				$this->load->model('localisation/language');
			
				$language = new Language($language_query->row['directory']);
				$language->load($language_query->row['filename']);	
				$language->load('mail/giftcard');
				
				$template = new Template();		
				
				$template->data['title'] = $language->get('text_title');				
				$template->data['text_greeting'] = sprintf($language->get('text_greeting'), $this->currency->formatPrice($giftcard['amount']));
				$template->data['text_from'] = sprintf($language->get('text_from'), $giftcard['from_name']);
				$template->data['text_message'] = $language->get('text_message');
				$template->data['text_redeem'] = sprintf($language->get('text_redeem'), $giftcard['code']);
				$template->data['text_footer'] = $language->get('text_footer');				
				
				$template->data['template'] = $this->config->get('config_template');
				$template->data['store_name'] = $this->config->get('config_name');
				$template->data['store_url'] = $this->config->get('config_url');
				
				$template->data['to'] = $giftcard['to_name'];
				$template->data['email'] = $giftcard['to_email'];
				$template->data['message'] = nl2br($giftcard['message']);
				$template->data['order_id'] = $order_id;
				$template->data['giftcard_code'] = $giftcard['code'];
				$template->data['amount'] = nl2br($giftcard['amount']);
	
				$html = $template->fetch('mail/giftcard.tpl');
				
				require_once(DIR_MAILER . 'mwc_mailer.php');        
				$mail = new MwcMailer(); 
				if($this->config->get('config_mail_protocol') == 'smtp'){           
					$mail->Host = $this->config->get('config_smtp_host');
					$mail->Username = $this->config->get('config_smtp_username');
					$mail->Password = $this->config->get('config_smtp_password');
					$mail->Port = $this->config->get('config_smtp_port');            
					
					$mail->IsSMTP();
				}
					
				$mail->setFrom($this->config->get('config_email'), $this->config->get('config_name'));                     
				$mail->Subject = sprintf($language->get('text_subject'), $giftcard['from_name']);
				$mail->MsgHTML($html);                
				$mail->AddAddress($giftcard['to_email']);
				$mail->send();  			
			}
		}
	}
}
?>